package org.viettan.android.main.activities.common;

import java.util.List;

import org.viettan.android.main.R;
import org.viettan.android.main.activities.articles.WebArticleActivity;
import org.viettan.android.main.intents.IntentKeys;
import org.viettan.android.main.io.DownloadEntityTask;
import org.viettan.android.main.io.EntityRetrievalException;
import org.viettan.android.main.managers.DialogManager;
import org.viettan.android.main.managers.SettingsManager;
import org.viettan.android.main.model.Article;
import org.viettan.android.main.model.Language;
import org.viettan.android.main.view.ArticleAdapter;
import org.viettan.android.main.view.ArticleAdapterFactory;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

import com.google.inject.Inject;

/**
 * Displays the latest Viet Tan news in a list and allows the user to select an article.
 */
public abstract class ArticleListActivity extends RefreshableListActivity {
    @Inject
    protected ArticleAdapterFactory articleAdaptorFactory;
    @Inject
    private DialogManager notificationManager;
    @Inject
    private SettingsManager settingsManager;

    private Language lastLanguage;

    /**
     * Default constructor required by child classes.
     */
    public ArticleListActivity() {
    }

    /**
     * Constructs a new {@code ArticleListActivity}.
     *
     * @param articleAdaptorFactory
     * @param notificationManager
     */
    protected ArticleListActivity(ArticleAdapterFactory articleAdaptorFactory,
	    DialogManager notificationManager) {
	this.articleAdaptorFactory = articleAdaptorFactory;
	this.notificationManager = notificationManager;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);

	loadList(true);
    }

    @Override
    protected void onRestart() {
	super.onRestart();
	if (settingsManager.hasLanguageChanged(lastLanguage)) {
	    loadList(true);
	}
    }

    @Override
    protected void loadList(boolean useCache) {
	lastLanguage = settingsManager.getArticleLanguage();
	new DownloadArticlesTask(this, notificationManager, useCache).execute();
    }

    protected void showArticles(List<Article> articles) {
	ArticleAdapter adapter = articleAdaptorFactory.create(this, R.layout.image_two_lines_row,
		articles);
	setListAdapter(adapter);
    }

    @Override
    protected void onListItemClick(ListView listView, View view, int position, long id) {
	Article article = (Article) getListAdapter().getItem(position);
	Intent articleIntent = new Intent(this, WebArticleActivity.class);
	articleIntent.putExtra(IntentKeys.ARTICLE_XML_URL.name(), article.getArticleXmlUrl());
	startActivity(articleIntent);
    }

    /**
     * Gets the list of {@link Article}s to display in the list.
     */
    protected abstract List<Article> getArticles(boolean useCache) throws EntityRetrievalException;

    private static class DownloadArticlesTask extends
	    DownloadEntityTask<ArticleListActivity, List<Article>> {

	private final boolean useCache;

	private DownloadArticlesTask(ArticleListActivity articleListActivity,
		DialogManager notificationManager, boolean useCache) {
	    super(articleListActivity, notificationManager);
	    this.useCache = useCache;
	}

	@Override
	protected List<Article> getEntity() throws EntityRetrievalException {
	    ArticleListActivity activity = getActivity();
	    if (activity == null) {
		return null;
	    }
	    return activity.getArticles(useCache);
	}

	@Override
	protected void displayEntity(List<Article> articles) {
	    ArticleListActivity activity = getActivity();
	    if (activity == null) {
		return;
	    }
	    activity.showArticles(articles);
	}
    }
}
